package com.xbfinal.ggkt.vod.controller;


import com.xbfinal.ggkt.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Api(tags = "文件管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/vod/file")
@Slf4j
public class FileController {




    /**
     * 上传教师头像
     * POST http://localhost:8301/admin/vod/file/upload
     */
    @PostMapping("upload")
    public Result upload(MultipartFile file){
        String Path="F:\\";
        //原始文件名
        final String originalFilename = file.getOriginalFilename();
        //获取 .jpg文件后缀
        final String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //随机名
        String uuidFileName= UUID.randomUUID().toString() +substring;
        //创建一个目录
        String basePath=Path+"讲师图片\\";
        File file1 = new File(basePath);
        //判断目录是否存在
        if(!file1.exists()){
            //目录不存在就创建
            file1.mkdirs();
        }
        //将文件转存
        try{
            log.info(basePath+uuidFileName);
            file.transferTo(new File(basePath+uuidFileName));

        }catch (IOException e){
            log.info("文件上传失败");
            return Result.fail("文件上传失败");
        }

        return Result.ok(basePath+uuidFileName);
    }

}
